﻿using Microsoft.AspNetCore.Components;
using Production.CommonLib.Models;
using Production.RazorLib.Services;
using Radzen;

namespace Production.RazorLib.Pages;

public partial class ProcessEdit
{
    private ProcessEditRequestDto _editRequestDto = new();

    [Inject] public IProcessService ProcessService { get; set; }

    [Inject] public DialogService DialogService { get; set; }

    [Parameter] public int Key { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new ProcessEditRequestDto();
        if (Key > 0)
        {
            var result = await ProcessService.GetProcess(Key);
            if (result != null && result.Succeeded && result.Data != null)
            {
                _editRequestDto.Code = result.Data.Code;
                _editRequestDto.Name = result.Data.Name;
                _editRequestDto.ProcessCategoryId = result.Data.ProcessCategoryId;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }


    void CancelButtonClick()
    {
        DialogService.Close();
    }

    async Task OnSubmit()
    {
        // await _editRequestForm.Validate();
        //
        // if (Key > 0)
        // {
        //     var result = await ProcessService.UpdateProcess(Key, _editRequestDto);
        //
        //     if (result.Succeeded)
        //     {
        //         MudDialog.Close(DialogResult.Ok(true));
        //     }
        // }
        // else
        // {
        //     var result = await ProcessService.CreateProcess(_editRequestDto);
        //
        //     if (result != null && result.Succeeded)
        //     {
        //         MudDialog.Close(DialogResult.Ok(true));
        //     }
        // }
    }
}